package com.hruiworks.campusgroup.controller;

import com.hruiworks.campusgroup.common.BaseResponse;
import com.hruiworks.campusgroup.enums.ErrorCode;
import com.hruiworks.campusgroup.enums.SuccessCode;
import com.hruiworks.campusgroup.exception.BusinessException;
import com.hruiworks.campusgroup.pojo.request.UserLoginRequest;
import com.hruiworks.campusgroup.pojo.request.UserRegisterRequest;
import com.hruiworks.campusgroup.pojo.request.UserUpdateRequest;
import com.hruiworks.campusgroup.pojo.vo.UserInfoVo;
import com.hruiworks.campusgroup.service.UserService;
import com.hruiworks.campusgroup.utils.AuthorityUtils;
import com.hruiworks.campusgroup.utils.ResultUtils;
import com.hruiworks.usercheck.pojo.entity.JwtEntity;
import com.hruiworks.usercheck.util.ParamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Objects;

/**
 * 用户相关controller
 *
 * @author jiushui
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 用户注册
     *
     * @param userRegisterRequest 用户注册需要的信息
     * @return 返回用户id
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (Objects.isNull(userRegisterRequest) || ParamUtils.isAnyBlank(userRegisterRequest)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long userId = userService.userRegister(userRegisterRequest.getUsername(),
                userRegisterRequest.getPassword(),
                userRegisterRequest.getCheckPassword(),
                userRegisterRequest.getUniversity());

        if (userId > 0) return ResultUtils.success(userId, SuccessCode.COMMON_SUCCESS);
        else throw new BusinessException(ErrorCode.OTHER_ERROR);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 用户登录需要的信息
     * @return 返回jwt凭证
     */
    @PostMapping("/login")
    public BaseResponse<JwtEntity> userLogin(@RequestBody UserLoginRequest userLoginRequest) {
        if (Objects.isNull(userLoginRequest) || ParamUtils.isAnyBlank(userLoginRequest)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        JwtEntity jwtEntity = userService.userLogin(userLoginRequest.getUsername(), userLoginRequest.getPassword());
        return ResultUtils.success(jwtEntity, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 获取当前登录用户的登录信息
     *
     * @param httpServletRequest http请求体
     * @return 当前用户的脱敏信息
     */
    @GetMapping("/current")
    public BaseResponse<UserInfoVo> currentUserInfo(HttpServletRequest httpServletRequest) {
        Long userId = AuthorityUtils.currentUser(httpServletRequest).getId();
        UserInfoVo userInfoVo = userService.currentUserInfo(userId);
        return ResultUtils.success(userInfoVo, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 更新用户信息
     *
     * @param userUpdateRequest 用户更新请求体
     * @param httpServletRequest http请求
     * @return 更新情况
     */
    @PostMapping("/update_info")
    public BaseResponse<Integer> updateUserInfo(@RequestBody UserUpdateRequest userUpdateRequest, HttpServletRequest httpServletRequest) {
        Long userId = AuthorityUtils.currentUser(httpServletRequest).getId();
        Integer result = userService.updateUser(userUpdateRequest, userId);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 注销账号
     *
     * @param httpServletRequest http请求体
     * @return 注销情况
     */
    @GetMapping("/terminate")
    public BaseResponse<Boolean> terminateAccount(HttpServletRequest httpServletRequest) {
        Long userId = AuthorityUtils.currentUser(httpServletRequest).getId();
        Boolean result = userService.deleteUserById(userId);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 上传头像
     * @param multipartFile 头像
     * @param httpServletRequest http请求体
     * @return 成功返回true
     */

    @PostMapping("/avatar/update")
    public BaseResponse<Object> updateAvatar(@RequestParam("avatar") MultipartFile multipartFile, HttpServletRequest httpServletRequest) throws IOException {
        Long userId = AuthorityUtils.currentUser(httpServletRequest).getId();
        boolean result = userService.updateUserAvatar(multipartFile, userId);

        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }





}
